import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'home',
  },
  {
    path: '/login',
    component:  () => import('views/Login')
  },
  {
    path: '/base',
    component:  () => import('views/Base'),
    redirect: 'home',
    children: [
      {
        path: '/home',
        component: () => import('views/system/home/Home'),
      },
      {
        path: '/chart',
        component: () => import('views/system/chart/Chart'),
      },
      {
        path: '/user',
        component: () => import('views/system/user/User')
      },
      {
        path: '/role',
        component: () => import('views/system/role/Role')
      },
      {
        path: '/menu',
        component: () => import('views/system/menu/Menu')
      },
      {
        path: '/notice',
        component: () => import('views/system/notice/Notice')
      },
      {
        path: '/notice/add',
        component: () => import('views/system/notice/NoticeAdd')
      },
      {
        path: '/notice/edit/:noticeId',
        component: () => import('views/system/notice/NoticeEdit')
      }
      ]
  }
]

const router = new VueRouter({
  // mode: 'history', //在nginx使用会有刷新问题
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from, next)=>{
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
